<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParams.id"
        :placeholder="'流程标识'"
        class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.name"
        :placeholder="'流程名称'"
        class="filter-item search-item"
      />

      <el-button class="filter-item" type="primary" plain @click="search">
        查询
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        重置
      </el-button>
      <el-dropdown
        v-has-any-permission="[
          'user:add',
          'user:delete',
          'user:reset',
          'user:export',
        ]"
        trigger="click"
        class="filter-item"
      >
        <el-button>
          更多 <i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-has-permission="['actReModel:add']"
            @click.native="add"
            >{{ $t("table.add") }}</el-dropdown-item
          >
          <el-dropdown-item
            v-has-permission="['actReModel:delete']"
            @click.native="batchDelete"
            >{{ $t("table.delete") }}</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%"
      @selection-change="onSelectChange"
    >
      <el-table-column type="selection" align="center" width="40px" />
      <el-table-column
        :label="'流程标识'"
        prop="id"
        :show-overflow-tooltip="true"
        align="center"
        min-width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程名称'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程类型'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.category }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程描述'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{
            scope.row.metaInfo | getDescription(scope.row.metaInfo)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'创建时间'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程版本'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.version }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="'操作'"
        align="center"
        min-width="200px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <i
            v-hasPermission="['user:view']"
            class="el-icon-view table-operation"
            style="color: #87d068"
            @click="view(row)"
            >查看</i
          >
          <i
            v-hasPermission="['actReModel:update']"
            class="el-icon-edit table-operation"
            style="color: #2db7f5"
            @click="edit(row)"
            >编辑</i
          >
          <i
            v-hasPermission="['actReModel:add']"
            class="el-icon-setting table-operation"
            style="color: #2db7f5"
            @click="openWorkflow(row)"
            >绘制</i
          >
          <i
            class="el-icon-position table-operation"
            style="color: #2db7f5"
            @click="publish(row)"
            >部署</i
          >
          <i
            class="el-icon-download table-operation"
            style="color: #2db7f5"
            @click="download(row)"
            >导出</i
          >
          <i
            v-hasPermission="['actReModel:delete']"
            class="el-icon-delete table-operation"
            style="color: #f50"
            @click="singleDelete(row)"
            >删除</i
          >
          <el-link
            v-has-no-permission="['user:view', 'user:update', 'user:delete']"
            class="no-perm"
          >
            {{ $t("tips.noPermission") }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.num"
      :limit.sync="pagination.size"
      @pagination="fetch"
    />
    <model-edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :title="dialog.title"
      @success="editSuccess"
      @close="editClose"
    />
    <model-view
      ref="view"
      :dialog-visible="userViewVisible"
      @close="viewClose"
    />
    <work-flow
      ref="workflow"
      :dialog-visible="workflowVisible"
      @close="workflowClose"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination';
import ModelEdit from './Edit';
import ModelView from './View';
import WorkFlow from '@/views/lol/workflow/workflow.vue';

export default {
  name: 'UserManage',
  components: { Pagination, ModelEdit, ModelView, WorkFlow },
  filters: {
    getDescription(obj) {
      // var description = "";
      // description = obj.match(/description":"(\S*)","revision/)[1];
      // return description
      var metaInfo = JSON.parse(obj);
      return metaInfo.description;
    }
  },
  data() {
    return {
      dialog: {
        isVisible: false,
        title: ''
      },
      userViewVisible: false,
      workflowVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      sort: {},
      selection: [],
      pagination: {
        size: 10,
        num: 1
      }
    };
  },
  computed: {
    currentUser() {
      return this.$store.state.account.user;
    }
  },
  // 生成页面
  mounted() {
    this.fetch();
  },
  methods: {
    download(row) {
      var queryObj = {
        modelId: row.id
      }
      this.$download(`activiti/service/modeler/export`, { ...queryObj })
    },
    publish(row) {
      var queryObj = {
        modelId: row.id
      }
      this.$get(`activiti/service/modeler/publish`, { ...queryObj }).then(() => {
        this.$message({
          message: this.$t('部署成功'),
          type: 'success'
        });
        this.search();
      });
    },
    workflowClose() {
      this.workflowVisible = false
    },
    viewClose() {
      this.userViewVisible = false;
    },
    editClose() {
      this.dialog.isVisible = false;
    },
    editSuccess() {
      this.search();
    },
    onSelectChange(selection) {
      this.selection = selection;
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      });
    },
    reset() {
      this.queryParams = {};
      this.sort = {};
      this.$refs.table.clearSort();
      this.$refs.table.clearFilter();
      this.search();
    },
    exportExcel() {
      this.$download(
        'system/user/excel',
        {
          pageSize: this.pagination.size,
          pageNum: this.pagination.num,
          ...this.queryParams
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    add() {
      this.dialog.title = this.$t('common.add');
      this.dialog.isVisible = true;
    },
    singleDelete(row) {
      this.$refs.table.toggleRowSelection(row, true);
      this.batchDelete();
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning'
        });
        return;
      }
      const contain = false;
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      })
        .then(() => {
          const flowIds = [];
          this.selection.forEach(u => {
            flowIds.push(u.id);
          });
          this.delete(flowIds);
        })
        .catch(() => {
          this.clearSelections();
        });
    },
    clearSelections() {
      this.$refs.table.clearSelection();
    },
    delete(flowIds) {
      this.loading = true;
      this.$delete(`activiti/service/actReModel/${flowIds}`).then(() => {
        this.$message({
          message: this.$t('tips.deleteSuccess'),
          type: 'success'
        });
        this.search();
      });
    },
    view(row) {
      this.$refs.view.setUser(row);
      this.userViewVisible = true;
    },
    edit(row) {
      var metaInfo = JSON.parse(row.metaInfo);
      row.description = metaInfo.description
      // row.description = row['metaInfo'].match(/description":"(\S*)","revision/)[1]
      this.$refs.edit.setUser(row);
      this.dialog.title = this.$t('common.edit');
      this.dialog.isVisible = true;
    },
    openWorkflow(row) {
      this.$refs.workflow.setUser(row);
      this.dialog.title = '流程设计';
      this.workflowVisible = true;
    },
    fetch(params = {}) {
      params.pageSize = this.pagination.size;
      params.pageNum = this.pagination.num;
      this.loading = true;
      this.$get('activiti/service/actReModel/list', {
        ...params
      }).then(r => {
        const data = r.data.resultBody;
        this.total = data.total;
        this.list = data.rows;
        this.loading = false;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
